package com.itheim.service.impl;

import com.itheim.mapper.DeptMapper;
import com.itheim.pojo.Dept;
import com.itheim.pojo.Emp;
import com.itheim.pojo.Result;
import com.itheim.pojo.Student;
import com.itheim.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> findall() {
        return deptMapper.findall();
    }

    @Override
    public void deletebyId(Integer id) {
        //检查班级里是否有学生
        List<Emp> empList=deptMapper.checkEmp(id);
        //删除班级
        if (empList.isEmpty()){
            deptMapper.deletebyId(id);
        }else {
            System.out.println(empList);
            throw new RuntimeException("部门里还有员工，不能删除");
        }

    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @Override
    public Dept getInfo(Integer id) {
        return deptMapper.selectbyId(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
